package vy.org.app.page.printlabel.report.instance;

import java.util.HashMap;
import java.util.Map;

import javax.management.InstanceAlreadyExistsException;

public class ReportManager {
	private static Map<Integer, ReportInstance> reports = new HashMap<Integer, ReportInstance>();
	private static ReportManager s_inst;
	
	public static ReportManager getInstance() {
		if(s_inst ==null)
			s_inst = new ReportManager();
		return s_inst;
	}
	
	public boolean createNewInstance(Integer reportId, String reportType) throws InstanceAlreadyExistsException {
		if(reports.containsKey(reportId)){
			throw new InstanceAlreadyExistsException("Report instance already exists :: "+ reportId);
		}
		ReportInstance rpt = ReportTypeFactory.createReportInstance(reportType, reportId);
		if(rpt != null){
			Thread t = new Thread(rpt);
			t.start();
			reports.put(reportId, rpt);
			
			return true;
		}else{
			return false;
		}
	}
	
	void remove(Integer reportId){
		reports.remove(reportId);
	}

	public void cancelRun(Integer reportId) {
		ReportInstance rpt = reports.get(reportId);
		rpt.stop();
	}
}
